package com.xt.partjobback.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.partjobback.constant.CommonConstant;
import com.xt.partjobback.constant.ResultDTO;
import com.xt.partjobback.entity.Job;
import com.xt.partjobback.entity.Message;
import com.xt.partjobback.exception.OperateException;
import com.xt.partjobback.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author leotong
 */
@RestController
@RequestMapping(CommonConstant.API_URI_VERSION_V1+"/message")
public class MessageController {

    private final MessageService messageService;
    @Autowired
    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    @PostMapping("/update")
    public ResultDTO<Boolean> updateMessage(@RequestBody Message message){
        return ResultDTO.success(messageService.updateById(message));
    }

    @GetMapping("/delete")
    public ResultDTO<Boolean> deleteMessage(Integer id){
        List<Message> list = messageService.list();
        if (list!=null&&list.size()==1){
            throw new OperateException("当前仅剩下一个公告禁止删除");
        }
        return ResultDTO.success(messageService.removeById(id));
    }

    @PostMapping("/createMessage")
    public ResultDTO<Boolean> createMessage(@RequestBody Message message){
        message.setCreateTime(new Date());
        return ResultDTO.success(messageService.save(message));
    }

    @PostMapping("/getMessage")
    public ResultDTO<Message> getMessage(){
        return ResultDTO.success(messageService.getMessage());
    }
    @PostMapping("/getMessageByPage")
    public ResultDTO<Page<Message>> getMessageByPage(@RequestBody Message message){
        return ResultDTO.success(messageService.getMessageByPage(message));
    }

}
